<template>
  <ex-dialog
    width="700px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="disabled">
        <el-row>
          <el-col :span="18">
            <el-form-item label="渠道名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请填写渠道名称" maxlength="20"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="渠道入口：" prop="channelType">
              <el-select v-model="ruleForm.channelType" filterable placeholder="请选择" size="small" @change="changeChannelType">
                <el-option v-for="item in channelList" :key="item.id" :label="item.value" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.channelType == 2">
          <el-col :span="18">
            <el-form-item label="店铺名称：" prop="businessId">
              <el-select v-model="ruleForm.businessId" filterable placeholder="请选择" size="small">
                <el-option v-for="item in shopList" :key="item.id" :label="item.value" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.channelType == 3">
          <el-col :span="18">
            <el-form-item label="赚卡名称：" prop="businessId">
              <el-select v-model="ruleForm.businessId" filterable placeholder="请选择" size="small">
                <el-option v-for="item in cardList" :key="item.cardId" :label="`(${item.goodsId})${item.cardName}`" :value="item.cardId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="button_box">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
      >保存提交</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  addPo,
  editPo,
  poDetail,
  shopSelect,
  cardSelect,
} from "@/api/merchantsNFR/channelManagement";
import '@wangeditor/editor/dist/css/style.css'
import {
  Editor,
  Toolbar
} from "@wangeditor/editor-for-vue";
export default {
  components: {
    Loading,
    Editor,
    Toolbar
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "新建渠道",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        businessId: '',
        channelType: '',//渠道入口 1 首页 2 店铺详情页 3 赚卡详情页 4 积分集市
        name: '',//渠道名称
      },
      channelList: [
        {id: '1',value: '首页'},
        {id: '2',value: '店铺详情页'},
        {id: '3',value: '赚卡详情页'},
        {id: '4',value: '积分集市'},
      ],
      shopList: [],//店铺下拉列表
      cardList: [],//赚卡下拉列表
      rules: {
        name: [
          { required: true,message: "请填写渠道名称",trigger: ["change", "bulr"]}
        ],
        channelType: [
          { required: true,message: "请选择",trigger: ["change", "bulr"]}
        ],
        businessId: [
          { required: true,message: "请选择",trigger: ["change", "bulr"]}
        ],
      },
    };
  },
  computed: {},
  created() {
    this.getShopSelect()
    this.getCardSelect()
    if(this.pathType == 'edit'){
      this.title = '编辑渠道'
      this.ruleForm.id = this.id
      this.getInfo()
    }
  },
  mounted() {
   
  },
  computed: {},
  methods: {
    // 获取店铺下拉 
    getShopSelect() {
      shopSelect().then(res => {
        this.shopList = res.result
      })
    },
    // 获取赚卡下拉 
    getCardSelect() {
      cardSelect().then(res => {
        this.cardList = res.result
      })
    },
    //获取详情
    getInfo(){
      var id = this.ruleForm.id
      poDetail(id).then(res => {
        this.ruleForm = {
          ...res.result,
          date: []
        }
      })
    },
    //切换选择渠道入口
    changeChannelType(){
      this.ruleForm.businessId = ''
    },
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.AgreeLoading = true
          var data = {
            ...this.ruleForm
          }
          if(this.pathType == 'add'){
            addPo(data).then(res => {
                this.handleClose()
                this.$message.success('创建成功')
                this.$emit('success')
                this.AgreeLoading = false
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }else if(this.pathType == 'edit'){
            editPo(data).then(res => {
              setTimeout(() => {
                this.handleClose()
                this.$message.success('修改成功')
                this.$emit('success')
                this.AgreeLoading = false
              }, 1000);
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}
.el-select,.el-input{
  width: 300px;
}
.main {
  padding: 20px 20px;
}
.button_box{
  display: flex;
  justify-content: center;
}
</style>
